const responseMessage = require("../response/response_message")
const jwt = require("jsonwebtoken")
const config = require("../config")

/**
 * 用户身份验证
 * @param {Object} ctx 
 * @param {Function} next 
 */
 exports.auth = async (ctx, next) => {
    const { token } = ctx.headers;
    if(!token){
        ctx.body = new responseMessage(403,"authentication failed",{})
    }else{
        try{
            const payoad = jwt.verify(token,config.screct);
            // ctx.request.body.payload  = payoad._doc;
            return await next();
        }catch(error){
            console.log(error.message);
            ctx.body = new responseMessage(403,"token expired",{})
        }
    }
}
